#!/usr/bin/env python3
# -*- coding:utf-8 -*-
from random import random

from utils import date_util


# 标题行
def get_a_head(head):
    return '<head> <meta charset="UTF-8"> <title>' + head + '</title> </head>'


# 获取一个链接按钮
def get_button_href(url, button_name):
    return '<a href="' + url + '"> <button>' + button_name + '</button> </a>'


# 获取一个按钮
def get_button(name):
    print('')


# 获取一个展示链表
def get_form_list(menu_name, name_dict):
    form_list = '<ul class="nav nav-pills nav-stacked custom-nav">'
    form_list += '<li><span>' + menu_name + '</span>'
    for key in name_dict.keys():
        value = name_dict[key]
        if isinstance(value, dict):
            form_list += get_form_list(key, value)
        elif isinstance(value, list):
            form_list += '<ul class="sub-menu-list"><span>' + key + '</span>'
            for value1 in value:
                form_list += '<li><a href="index.html">' + value1 + '</a></li>'
            form_list += '</ul>'
        else:
            form_list += '<li class="active"><a href="index.html">' + key + '</a></li>'
    form_list += '</li>'
    form_list += '</ul>'
    return form_list


# 获取一个可跳转链表
def get_form_list_actual(menu_name, name_dict):
    form_list = '<ul class="nav nav-pills nav-stacked custom-nav">'
    form_list += '<li><span>' + menu_name + '</span>'
    for key in name_dict.keys():
        value = name_dict[key]
        if isinstance(value, dict):
            form_list += get_form_list_actual(key, value)
        elif isinstance(value, list):
            form_list += '<ul class="sub-menu-list"><span>' + key + '</span>'
            for value1 in value:
                if isinstance(value1, list):
                    if len(value1) == 2:
                        form_list += '<li><a href="' + value1[1] + '">' + value1[0] + '</a></li>'
                    if len(value1) == 3:
                        form_list += '<li>'
                        form_list += '<div id="main" style="width:600px;" >'
                        form_list += '   <div id="left" style="width:100px;float:left;" >'
                        form_list += '   <a href="' + value1[1] + '">' + value1[0] + '</a></div>'
                        form_list += '   <div id="right" style="width:500px;float:left;" >' + value1[2] + '</div>'
                        form_list += '</div>'
                        form_list += '</li>'
                elif isinstance(value1, dict):
                    if 'url' in value1.keys():
                        form_list += '<li><a href="' + value1['url'] + '">' + value1['name'] + '</a></li>'
                else:
                    form_list += '<li>' + value1 + '</li>'
            form_list += '</ul>'
        else:
            form_list += '<li>' + key + '</li>'
    form_list += '</li>'
    form_list += '</ul>'
    return form_list


# 获取一个展示链表含有按钮
def get_form_list_has_button(menu_name, name_dict):
    form_list = '<ul class="nav nav-pills nav-stacked custom-nav">'
    form_list += '<li><span>' + menu_name + '</span>'
    for key in name_dict.keys():
        value = name_dict[key]
        if 'url' in value.keys():
            form_list += '<ul class="sub-menu-list"><text>' + value['title'] + '</text>'
            form_list += '<li><a href="' + value['url'] + '"> '
            form_list += '<button>' + value['button'] + '</button> </a></li>'
            form_list += '</ul>'
        else:
            form_list += get_form_list_has_button(key, value)
    form_list += '</li>'
    form_list += '</ul>'
    return form_list


# 获取一个展示链表含有按钮
def get_form_has_buttons(menu_name, name_dict_list):
    form_list = '<ul class="nav nav-pills nav-stacked custom-nav">'
    form_list += '<li><span>' + menu_name + '</span>'
    for name_dict in name_dict_list:
        for key in name_dict.keys():
            value = name_dict[key]
            if 'url' in value.keys():
                form_list += '<ul class="sub-menu-list"><text>' + value['title'] + '</text>'
                form_list += '<li><a href="' + value['url'] + '"> '
                form_list += '<button>' + value['button'] + '</button> </a></li>'
                form_list += '</ul>'
            else:
                form_list += get_form_has_buttons(key, value)
    form_list += '</li>'
    form_list += '</ul>'
    return form_list


# 获取一个标题
def get_title(name):
    title = '<h><font size="4" align="center" style="font-family:Microsoft YaHei" color="black">' + name + '</font></h>'
    return title


# 获取一个标题
def get_a_text(name):
    text = '<text>' + name + '</text>'
    return text


# 获取一个表格
def get_form(action, method, form_name, row_name_dict, submit_name):
    form = ' <form action="' + action + '" method="' + method + '" name="' + form_name + '">'
    for key in row_name_dict:
        form += key + ': <input type="text" name="' + row_name_dict[key] + '"><br>'
    form += '    <input type="submit" value="' + submit_name + '">'
    form += ' </form>'
    return form


# 获取一个隐藏表格(打开新页面)
def get_form_hidden(action, method, form_name, row_dict, submit_name):
    form = ' <form action="' + action + '" method="' + method + '" name="' + form_name + '" style="PADDING-RIGHT: 1px; PADDING-LEFT: 1px; PADDING-BOTTOM: 1px; MARGIN: 1px; PADDING-TOP: 1px" target="_blank" >'
    for key in row_dict:
        value = row_dict[key]
        if isinstance(value, list):
            if len(value) == 2:
                form += '<input type="hidden" size = "10" name="' + value[0] + '" value="' + value[1] + '"> '
            elif len(value) == 1:
                form += '<input type="hidden" size = "10" name="' + value[0] + '"> '
        elif isinstance(value, dict):
            form += '<input type="hidden" size = "10" name="' + value['name'] + '" value="' + value['value'] + '"> '
        else:
            form += '<input type="hidden" size = "10" name="' + value + '"> '
    form += '    <input type="submit" value="' + submit_name + '">'
    form += ' </form>'
    return form


# 获取一个表格
# 0:不打开新页面
def get_form_hidden_blank(action, row_dict: dict, submit_name, target: int):
    return get_form_with_config(action, row_dict, submit_name, target, 10)


def get_form_size(action, row_dict: dict, submit_name, size: int):
    return get_form_with_config(action, row_dict, submit_name, 0, size)


# 根据配置获取一个表格
# row_dict: {
#         	'用户编码': {
#         		'value': whm,
#         		'name': 'usercode',
#         		'type': 'hidden'
#         	},
#         	'股票编码': {
#         		'value': '000002',
#         		'name': 'stockcode',
#         		'type': 'text'
#         	}
# action        跳转连接
# submit_name   提交按钮名称
# size          展示文本的长度
# target        0:调转原网页 1:跳转新的空白网页
def get_form_with_config(action, row_dict: dict, submit_name, target: int, size: int):
    if target == 0:
        form = ' <form action="' + action + '" method="post" style="PADDING-RIGHT: 1px; PADDING-LEFT: 1px; PADDING-BOTTOM: 1px; MARGIN: 1px; PADDING-TOP: 1px" name="form_name" >'
    else:
        form = ' <form action="' + action + '" method="post" style="PADDING-RIGHT: 1px; PADDING-LEFT: 1px; PADDING-BOTTOM: 1px; MARGIN: 1px; PADDING-TOP: 1px" name="form_name" target="_blank" >'
    for key in row_dict:
        value = row_dict[key]
        if 'type' not in value.keys():
            value['type'] = 'text'
        form += '<input type="' + value['type'] + '" size = "' + str(size) + '" name="' + value['name'] + '" value="' + \
                value['value'] + '"> '
    form += '    <input type="submit" value="' + submit_name + '">'
    form += ' </form>'
    return form


# 根据配置获取一个表格
# row_dict: {
#         	'用户编码': {
#         		'value': whm,
#         		'name': 'usercode',
#         		'type': 'hidden',
#         		'cross': 0
#         		'label': se
#         	},
#         	'股票编码': {
#         		'value': '000002',
#         		'name': 'stockcode',
#         		'type': 'text',
#         		'cross': 1
#         		'cross': 'input'
#         	}
# action        跳转连接
# row_dict      拼接的参数
# submit_name   提交按钮名称
# size          展示文本的长度
# target        0:调转原网页 1:跳转新的空白网页
# cross         0:每个值换行 1:每个input不换行
# describe      0:无每个输入值描述 1:key值作为描述值
# in_line       0:无处理 1:和其他表格并列一行
def get_form_config1(action, row_dict: dict, submit_name, target: int, size: int, cross: int, describe: int,
                     in_line: int):
    style = 'style="PADDING-RIGHT: 1px; PADDING-LEFT: 1px; PADDING-BOTTOM: 1px; MARGIN: 1px; PADDING-TOP: 1px "'
    if in_line == 1:
        style = style[0:len(style) - 1] + '; display:inline"'
    target_value = ''
    if target == 1:
        target_value = 'target="_blank"'
    form = ' <form action="' + action + '" id = ' + 'f' + str(int(random() * 10000)) + \
           '" method="post" ' + style + 'name="form_name" ' + target_value + ' >'
    for key in row_dict:
        value = row_dict[key]
        if 'type' not in value.keys():
            value['type'] = 'text'
        if describe == 1 and value['type'] != 'hidden':  # 隐藏的无操作
            form += key + ':'
        form += '<input type="' + value['type'] + '" size = "' + str(size) + '" name="' + value['name'] + '" value="' + \
                value['value'] + '"> '
        if cross == 0 and value['type'] != 'hidden':  # 隐藏的不换行
            form += '<br>'
    form += '    <input type="submit" value="' + submit_name + '">'
    form += ' </form>'
    return form


# 获取一个分页索引
# 只有交易日的分页
def get_page_date_index(action, row_dict, show_date: str, show_num: int):
    page_list = ''
    date_list = date_util.find_valid_days(show_date, -1, True, int(show_num / 2)) + \
                [show_date] + date_util.find_valid_days(show_date, 1, True, int(show_num / 2))
    for i in range(show_num):
        page_list += ' <form action="' + action + '" method="post" style="PADDING-RIGHT: 1px; PADDING-LEFT: 1px; PADDING-BOTTOM: 1px; MARGIN: 1px; PADDING-TOP: 1px; display: inline" name="form_name" >'
        for key in row_dict:
            value = row_dict[key]
            page_list += '<input type="hidden" name="' + value['name'] + '" value="' + date_list[i] + '"> '
        if date_list[i] == show_date:
            page_list += '    <input type="submit" value="' + date_list[i][
                                                              -5:] + '" style="width:55px;height:28px;PADDING-RIGHT: 1px; PADDING-LEFT: 1px; PADDING-BOTTOM: 1px; MARGIN: 1px; PADDING-TOP: 1px;font-size:18px;" >'
        else:
            page_list += '    <input type="submit" value="' + date_list[i][-5:] + '">'
        page_list += ' </form>'
    return page_list


# 获取一个分页索引
# row_dict : 展示通用内容
# page_key : 变化的key值
# pages : 展示页码 [1,2,3,4,5,6]
# show_page : 当前展示页 3
def get_page_index(action, row_dict, page_key, pages: list, show_page: int):
    page_list = ''
    for page in pages:
        page_list += ' <form action="' + action + '" method="post" style="PADDING-RIGHT: 1px; PADDING-LEFT: 1px; PADDING-BOTTOM: 1px; MARGIN: 1px; PADDING-TOP: 1px; display: inline" name="form_name" >'
        for key in row_dict:
            value = row_dict[key]
            if page_key == key:
                page_list += '<input type="hidden" name="' + value['name'] + '" value="' + str(page) + '"> '
            else:
                page_list += '<input type="hidden" name="' + value['name'] + '" value="' + value['value'] + '"> '
        if page == show_page:
            page_list += '<input type="submit" value="' + str(
                page) + '" style="width:55px;height:28px;PADDING-RIGHT: 1px; PADDING-LEFT: 1px; PADDING-BOTTOM: 1px; MARGIN: 1px; PADDING-TOP: 1px;font-size:18px;" >'
        else:
            page_list += ' <input type="submit" value="' + str(page) + '">'
        page_list += ' </form>'
    return page_list


# 获取一个横向
def get_form_cross(action, method, form_name, row_name_dict, submit_name):
    form = ' <form action="' + action + '" method="' + method + '" name="' + form_name + '">'
    for key in row_name_dict:
        form += key + ': <input type="text" size = "10" name="' + row_name_dict[key] + '">'
    form += '    <input type="submit" value="' + submit_name + '">'
    form += ' </form>'
    return form


# 获取一个表格
def get_form_has_value(action, method, form_name, row_dict, submit_name):
    form = ' <form action="' + action + '" method="' + method + '" name="' + form_name + '"  target="_blank"  >'
    for key in row_dict:
        value = row_dict[key]
        if isinstance(value, list):
            if len(value) == 2:
                form += key + ': <input type="text" name="' + value[0] + '" value="' + value[1] + '"> '
            elif len(value) == 1:
                form += key + ': <input type="text" name="' + value[0] + '"> '
        elif isinstance(value, dict):
            form += key + ': <input type="text" name="' + value['name'] + '" value="' + value['value'] + '"> '
        else:
            form += key + ': <input type="text" name="' + value + '"> '
    form += '    <input type="submit" value="' + submit_name + '">'
    form += ' </form>'
    return form


# 获取一个表格
def get_form_text(action, method, form_name, submit_name):
    form = ' <form action="' + action + '" method="' + method + '" name="' + form_name + '"  target="_blank"  >'
    form += '<div>'
    form += '输入文本:'
    form += '            <textarea cols="100" rows="3" name="data" placeholder="输入文本"></textarea>'
    form += '</div>'
    form += '    <input type="submit" value="' + submit_name + '">'
    form += ' </form>'
    return form


# 获取一个表格
# content_arr : 二维数组
def get_a_table(content_arr):
    table = '<table border="1">'
    for row in content_arr:
        table += '<tr>'
        for cell in row:
            table += '<th>' + str(cell) + '</th>'
        table += '</tr>'
    table += '</table>'
    return table


# 配置中所需要的常量
class ConfCons:
    id = 'id'
    key = 'key'
    name = 'name'
    type = 'type'
    list = 'list'
    form = 'form'
    size = 'size'
    conf = 'conf'
    label = 'label'
    cross = 'cross'
    value = 'value'
    clazz = 'class'
    describe = 'describe'
    selected = 'selected'
    placeholder = 'placeholder'


# 获取一个嵌套在form中的下拉列表
def get_select_conf(name, form_id, option_list):
    select = '<select name="' + name + '" form="' + form_id + '">'
    for option in option_list:
        selected = ''
        if ConfCons.selected in option.keys():
            selected = 'selected="selected"'
        select += '<option value="' + option.get(ConfCons.value) + '"' + \
                  selected + '>' + option.get(ConfCons.key) + '</option>'
    select += '</select>'
    return select
    # <form action="/demo/demo_form" id="carform">
    #   名字：<input type="text" name="fname">
    #     <select name="carlist" form="carform">
    # 	  <option value="volvo">Volvo</option>
    # 	  <option value="saab">Saab</option>
    # 	  <option value="opel">Opel</option>
    # 	  <option value="audi">Audi</option>
    # 	</select>
    #   <input type="submit">
    # </form>


# 获取一个input标签
# placeholder初始提示
def get_input_conf(placeholder: str, option_list):
    print('生成一个input标签')
    input = '<input type="text" th:field="*{sex}" list="listItem" placeholder="' + placeholder + '">'
    input += '<datalist id="listItem">'
    for option in option_list:
        input += '<option value="' + option.get(ConfCons.value) + '" >' + ConfCons.key + '</option>'
    input += '</datalist>'
    return input
    # <input type="text" th:field="*{sex}" list="listItem" placeholder="请选择性别">
    # <datalist id="listItem">
    #         <option value="1" >男</option>
    #         <option value="2" >女</option>
    # </datalist>


# 根据配置获取一个表格
# row_dict: {
#             '买卖方式': {
#                 'label': 'selected',
#                 'name': 'buyprice',
#                 'conf': {
#                     'option_list': [{'key': '买入', 'value': '1'},
#                                     {'key': '卖出', 'value': '2'}, ]
#                 }
#             },
#             '价格关注方式': {
#                 'label': 'selected',
#                 'name': 'buyprice',
#                 'conf': {
#                     'option_list': [{'key': '低于', 'value': '1'},
#                                     {'key': '高于', 'value': '2'}, ]
#                 }
#             },
#             '输入价格': {
#                 'value': '2116.88',
#                 'name': 'stockcode',
#                 'type': 'text',
#                 'size': 10,
#                 'describe': 0
#             },
#             '消息提示': {
#                 'value': '请输入触发提示的扩展信息',
#                 'name': 'stockcode',
#                 'type': 'text',
#                 'size': 30,
#             },
#         }
# action        跳转连接
# row_dict      拼接的参数
# submit_name   提交按钮名称
# size          展示文本的长度
# target        0:调转原网页 1:跳转新的空白网页
# cross         0:每个值换行 1:每个input不换行
# describe      0:无每个输入值描述 1:key值作为描述值
# in_line       0:无处理 1:和其他表格并列一行
def get_form_conf(action, row_dict: dict, submit_name, target: int, size: int, cross: int, describe: int, in_line: int):
    style = ' style="PADDING-RIGHT: 1px; PADDING-LEFT: 1px; PADDING-BOTTOM: 1px; MARGIN: 1px; PADDING-TOP: 1px "'
    if in_line == 1:
        style = style[0:len(style) - 1] + '; display:inline"'
    target_value = ''
    if target == 1:
        target_value = 'target="_blank"'
    form_id = 'f' + str(int(random() * 10000))
    form = ' <form action="' + action + '" id = "' + form_id + '" method="post" ' + style + 'name="form_name" ' + target_value + ' >'
    for key in row_dict:
        value = row_dict[key]
        if ConfCons.label not in value.keys() or \
                (ConfCons.label in value.keys() and value.get(ConfCons.label) == 'input'):
            # 设置输入类型
            if 'type' not in value.keys():
                value['type'] = 'text'  # 设置默认为text
            # 是否展示key属性
            if (ConfCons.describe in value.keys() and value.get(ConfCons.describe) == 1) or \
                    (describe == 1 and value['type'] != 'hidden' and ConfCons.describe not in value.keys()):
                form += key + ':'
            # 设置input长度尺寸
            input_size = size
            if ConfCons.size in value.keys():
                input_size = value.get(ConfCons.size)
            form += '<input type="' + value['type'] + \
                    '" size = "' + str(input_size) + \
                    '" name="' + value['name'] + \
                    '" value="' + value['value'] + '"> '
            # 是否换行 隐藏的不换行
            if (cross == 0 and value['type'] != 'hidden') or \
                    ('cross' in value.keys() and value.get(ConfCons.cross) == 0):
                form += '<br>'
        elif ConfCons.label in value.keys() and value.get(ConfCons.label) == 'selected':
            form += get_select_conf(value.get('name'), form_id, value.get(ConfCons.conf).get('option_list'))
    form += '    <input type="submit" value="' + submit_name + '">'
    form += ' </form>'
    return form


if __name__ == "__main__":
    print('')
    print(get_page_date_index('a', {}, date_util.get_date_str(), 3))
